package com.yycx.module.bus.provider.handler.haiying.settle.pay;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.yycx.bpm.provider.common.CommentTypeEnum;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.export.DictDataVo;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseRoleService;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.BusHyorderReturn;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.flowable.engine.TaskService;
import org.flowable.engine.task.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 结算申请详情列表
 *
 * @author zyf
 * @view src/views/module/haiying/order/settle/components/apply_details.vue
 */
@Component
public class HySettleDetailsPageListHandler implements PageInterceptor<BusHyorderReturn>, ExportInterceptor {

    @Resource
    private BusPaymentService paymentService;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TaskService taskService;
    @Autowired
    private BaseUserService userService;
    @Autowired
    private BaseRoleService roleService;

    @Override
    public void prepare(CriteriaQuery<BusHyorderReturn> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        Long paymentId = params.getLong("paymentId");
        ApiAssert.isNotEmpty("参数异常", paymentId);
        BusPayment payment = paymentService.getById(paymentId);
        Integer settleType = payment.getSettleType();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String orderNo = params.get("orderNo");
        String saleManName = params.get("saleManName");
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = openUser.getCompanyId();
        cq.addSelect(HyOrder.class, "orderId", "outOrderNo","sendDate","orderType", "orderNo", "businessArea", "businessCode", "sendType", "customBondType", "saleManName");
        cq.addSelect(BusHyorderReturn.class, "*");
        cq.addSelect(BusCustom.class, "customName", "settleType");
        cq.addSelect(BusDept.class, "deptName");
        cq.eq("ho.orderState", CommonConstants.INT_2);
        //业务员
        cq.like("ho.saleManName", saleManName);
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        cq.like(BusCustom.class, "customName");
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "ho.sendDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "ho.sendDate", endDate);
        if (settleType.equals(1)) {
            cq.eq(true, "bhr.settleApplyId", paymentId);
        }
        if (settleType.equals(2)) {
            cq.eq(true, "bhr.settleApplyIdD", paymentId);
        }
        if (settleType.equals(3)) {
            cq.eq(true, "bhr.settleApplyIdC", paymentId);
        }
        if (settleType.equals(4)) {
            cq.eq(true, "bhr.settleApplyIdE", paymentId);
        }
        cq.eq(true, "ho.companyId", companyId);
        cq.eq(HyOrder.class, "deptId");
        cq.createJoin(BusCustom.class);
        cq.createJoin(HyOrder.class);
        cq.createJoin(BusDept.class);
        cq.orderByDesc("ho.sendDate");
        //传递参数
        params.put("settleType", payment.getSettleType());
        params.put("paymentNo", payment.getPaymentNo());
    }

    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        if (key.equals("deptType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("自营");
            dictDataVo.setValue("ZY");
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("招商");
            dictDataVo2.setValue("ZS");
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
        if (key.equals("sendType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("现款");
            dictDataVo.setValue(1);
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("预付款");
            dictDataVo2.setValue(2);
            DictDataVo dictDataVo3 = new DictDataVo();
            dictDataVo3.setLabel("资保金");
            dictDataVo3.setValue(3);
            DictDataVo dictDataVo4 = new DictDataVo();
            dictDataVo4.setLabel("资信");
            dictDataVo4.setValue(4);
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            dictDataVos.add(dictDataVo3);
            dictDataVos.add(dictDataVo4);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
    }

    @Override
    public TemplateExportParams initTemplateExportParams(Map params) {
        Integer settleType = MapUtil.getInt(params, "settleType",1);
        TemplateExportParams templateExportParams = new TemplateExportParams("excel/结算申请明细" + settleType + ".xlsx", true);
        return templateExportParams;
    }

    @Override
    public void initExportData(Map dataMap, List list,CriteriaQuery cq) {
        dataMap.put("nickName", OpenHelper.getNickName());
        dataMap.put("applyDate", DateUtil.format(DateUtil.date(), "yyyy-MM-dd"));
        String processInstanceId = request.getParameter("processInstanceId");
        Long companyId = OpenHelper.getCompanyId();
        String paymentNo= cq.getParams("paymentNo");
        dataMap.put("paymentNo",paymentNo);
        if(companyId.equals(5L)){
            dataMap.put("companyName","丹博瑞");
        }
        if(companyId.equals(1018L)){
            dataMap.put("companyName","赤壁医药");
        }
        //查询审批人
        if (FlymeUtils.isNotEmpty(processInstanceId)) {
            List<Comment> commentsList = taskService.getProcessInstanceComments(processInstanceId, CommentTypeEnum.WC.toString());
            if (FlymeUtils.isNotEmpty(list)) {
                commentsList = commentsList.stream().filter(ConvertUtils.distinctByKey(Comment::getUserId)).collect(Collectors.toList());
                for (Comment comment : commentsList) {
                    String userId = comment.getUserId();
                    BaseUser baseUser = userService.getUserById(Long.parseLong(userId));
                    List<String> roleCodes = roleService.selectRoleCodesByUserId(baseUser.getUserId());
                    if (roleCodes.contains("ROLE_CW_JSY")) {
                        dataMap.put("nickName_cw_jsy", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_FHY")) {
                        dataMap.put("nickName_cw_fhy", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_SW_SCZZ")) {
                        dataMap.put("nickName_sw_sczz", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_SW_FHY")) {
                        dataMap.put("nickName_sw_fhy", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_JSBX")) {
                        dataMap.put("nickName_jsbz", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_CWBZ")) {
                        dataMap.put("nickName_cwbz", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_CW_ZJ")) {
                        dataMap.put("nickName_cwzj", baseUser.getNickName());
                    }
                    if (roleCodes.contains("ROLE_FLP_ZJL")) {
                        dataMap.put("nickName_zjl", baseUser.getNickName());
                    }
                }
            }
        }
    }

}
